from django import forms
from .models import CustomUser
from captcha.fields import CaptchaField,CaptchaTextInput
from main.models import Article
from ckeditor.widgets import CKEditorWidget


class UserPassWordChangeForm(forms.Form):
    old_password = forms.CharField(label="原始密码", widget=forms.PasswordInput(attrs={
        "class": "form-control",
        "placeholder": "请输入密码"
    }))
    password = forms.CharField(label="密码", widget=forms.PasswordInput(attrs={
        "class": "form-control",
        "placeholder": "请输入密码"
    }))
    password2 = forms.CharField(label="确认密码", widget=forms.PasswordInput(attrs={
        "class": "form-control",
        "placeholder": "请再次输入密码"
    }))

    def clean_password(self):
        password = self.cleaned_data.get('password')
        if len(password) < 6:
            raise forms.ValidationError("密码长度要大于6位")
        return password

    def clean_password2(self):
        password = self.cleaned_data.get('password')
        password2 = self.cleaned_data.get('password2')
        if password != password2:
            raise forms.ValidationError("密码不一致")
        return password2

class UserLoginForm(forms.Form):
    username = forms.CharField(label="用户名/邮箱", widget=forms.TextInput(attrs={
        "class": "form-control",
        "placeholder": "请输入用户名/邮箱"
    }))
    password = forms.CharField(label="密码", widget=forms.PasswordInput(attrs={
        "class": "form-control",
        "placeholder": "请输入密码"
    }))


class UserRegisterForm(forms.ModelForm):
    password = forms.CharField(label="密码", widget=forms.PasswordInput(attrs={
        "class": "form-control",
        "placeholder": "请输入密码",
        "minlength": "6"
    }))
    password2 = forms.CharField(label="确认密码", widget=forms.PasswordInput(attrs={
        "class": "form-control",
        "placeholder": "请再次输入密码"
    }))
    # 添加验证码字段
    captcha = CaptchaField(label="验证码", widget=CaptchaTextInput(attrs={
        "class": "form-control",
        "placeholder": "请输入验证码",
        "style": "display: inline-block; width: 150px;"
    }))


    class Meta:
        model = CustomUser
        fields = ['username', 'email']
        widgets = {
            'username': forms.TextInput(attrs={
                "class": "form-control",
                "placeholder": "请输入用户名",
                "minlength": "3"
            }),
            'email': forms.EmailInput(attrs={
                "class": "form-control",
                "placeholder": "请输入邮箱"
            }),
        }

    def clean_username(self):
        username = self.cleaned_data.get('username')
        if CustomUser.objects.filter(username=username).exists():
            raise forms.ValidationError("用户名已经存在")
        return username
        
    def clean_email(self):
        email = self.cleaned_data.get('email')
        if CustomUser.objects.filter(email=email).exists():
            raise forms.ValidationError("邮箱已经存在")
        return email

    def clean_password(self):
        password = self.cleaned_data.get('password')
        if len(password) < 6:
            raise forms.ValidationError("密码长度要大于6位")
        # 添加密码强度验证
        if not any(char.isdigit() for char in password):
            raise forms.ValidationError("密码必须包含数字")
        return password

    def clean_password2(self):
        password = self.cleaned_data.get('password')
        password2 = self.cleaned_data.get('password2')
        if password != password2:
            raise forms.ValidationError("两次输入的密码不一致")
        return password2




class UserInfoChangeForm(forms.ModelForm):
    class Meta:
        model = CustomUser
        fields = ['head']
        widgets = {
            'head': forms.FileInput(attrs={})
        },
        labels = {
            'head': "头像"
        }

    def clean_head(self):
        head = self.cleaned_data.get('head')
        return head



class ArticleForm(forms.ModelForm):
    class Meta:
        model = Article
        fields = ['title', 'content', 'tags', 'categorys']
        widgets = {
            'title': forms.TextInput(attrs={'class': 'form-control'}),
            # 'content': forms.Textarea(attrs={'class': 'form-control'}),
            'content': CKEditorWidget(attrs={'class': 'form-control'}),
            'tags': forms.SelectMultiple(attrs={'class': 'form-control'}),
            'categorys': forms.SelectMultiple(attrs={'class': 'form-control'}),
        }


